package cn.melonlib.cores.graphql.comp.config.impl;

import cn.melonlib.cores.commons.model.entity.IEntity;
import cn.melonlib.cores.graphql.comp.config.WorkerParametersReader;
import cn.melonlib.cores.graphql.config.bean.ParameterReader;
import cn.melonlib.cores.graphql.config.bean.impl.*;
import cn.melonlib.cores.graphql.model.input.GraphQLOrderBy;
import cn.melonlib.cores.model.input.OrderByInput;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Component("defaultWorkerParametersReader")
@ConditionalOnProperty(value = "system.worker.config.init.readers",havingValue = "default",matchIfMissing = true)
public class DefaultWorkerParametersReader extends ApplicationObjectSupport implements WorkerParametersReader {

    private LinkedHashMap<Class<?extends Object>, ParameterReader> parameterReaders = new LinkedHashMap<>();
    @javax.annotation.Resource
    private IntegerReader integerReader;

    @javax.annotation.Resource
    private StringReader stringReader;

    @javax.annotation.Resource
    private MapReader mapReader;

    @javax.annotation.Resource
    private EntityReader entityReader;

    @javax.annotation.Resource
    private ListReader listReader;

    @javax.annotation.Resource
    private FloatReader floatReader;

    @Resource
    private BooleanReader booleanReader;

    @javax.annotation.Resource
    private OrderByReader orderByReader;

    @Resource
    private OrderByInputReader orderByInputReader;



    @Override
    @PostConstruct
    public void init() {
        this.parameterReaders.put(Integer.class, integerReader);
        this.parameterReaders.put(String.class, stringReader);
        this.parameterReaders.put(Map.class, mapReader);
        this.parameterReaders.put(IEntity.class, entityReader);
        this.parameterReaders.put(List.class, listReader);
        this.parameterReaders.put(Float.class, floatReader);
        this.parameterReaders.put(Double.class, floatReader);
        this.parameterReaders.put(Long.class, floatReader);
        this.parameterReaders.put(Boolean.class, booleanReader);
        this.parameterReaders.put(GraphQLOrderBy.class,orderByReader);
        this.parameterReaders.put(OrderByInput.class,orderByInputReader);
    }

    @Override
    public <PR extends ParameterReader> PR getReader(Class paramType) {
        return (PR) this.parameterReaders.get(paramType);
    }

    @Override
    public void registerReader(Class<?> paramType, ParameterReader<?> reader) {
        this.parameterReaders.put(paramType, reader);
    }
}
